/**
 * Created by Dave on 2017/3/14.
 */
module.controller('ConfigFileSetListController',
    function ($scope, $state, $stateParams, $cookies,$timeout,$http,$mdDialog,configurateService,$filter) {
        var tabs = [
            { title: '默认配置组',value:'default'}
        ];
        $scope.selectedIndex = 0 ; //initializing is a good manner
        $scope.currtab= tabs?tabs[0] : {};//当前选中的tab
        $scope.tabs = tabs;

        $scope.allConfigFile={};
        $http({
            url:'/appConfig/listConfigFileSet',
            method:'GET'
        }).success(function(data,header,config,status){
            //响应成功
            $scope.configFileSets=data;
          $scope.allConfigFile=data
            // console.log(JSON.stringify(data));

        }).error(function(data,header,config,status){
        });

      $scope.searchPageHide=false;



        $scope.currentPageCfgSet = [];
        $scope.currentPage = 1;
        $scope.numPerPage = 10;
      $scope.searchText="";

      $scope.$watch("configFileSets + currentPage", function () {
            if ($scope.configFileSets) {
                $scope.currentPageCfgSet = [];
                var begin = (($scope.currentPage - 1) * $scope.numPerPage)
                    , end = begin + $scope.numPerPage;

                if($scope.searchText == ""){
                  $scope.currentPageCfgSet = $scope.configFileSets.slice(begin, end);
                }else{
                  $scope.currentPageCfgSet = $filter('filter')($scope.configFileSets, {'cfgMgrId': $scope.searchText})
                }
            }
          $scope.searchClick=function () {
            /*       $scope.currentPageServices=utilService.searchObj(currentServices,$scope.services,$scope.searchText,'id')*/

            if ($scope.searchText == "") {
              $scope.searchPageHide=false;

              $scope.currentPageCfgSet = $scope.configFileSets.slice(begin, end);
            } else {
              $scope.searchPageHide=true;
              $scope.currentPageCfgSet = $filter('filter')($scope.configFileSets, {'cfgMgrId': $scope.searchText})
            }
          }
        });

        $scope.addVersion=function (e,item) {
            //$scope.custom=true;
            e.stopPropagation();
            //console.log("add ver:"+index);
            $state.go('cp.editConfigFileSet',{"cfs":{"op":"addver",isMarket:item.isMarket,imageName:item.cfgMgrId,verList:item.versionList}});
        }

        $scope.mgrCfgEnv = function (imageName,version,e) {
          e.stopPropagation();
          $mdDialog.show({
          controller:'configAddController',
          templateUrl: 'configurationVersion.html',
          parent: angular.element(document.body),
          //targetEvent: ev,
          clickOutsideToClose: true,
          fullscreen: true, // Only for -xs, -sm breakpoints.
          locals: {
            params: {
                imageName: imageName,
                version:version
            }
          },
        })
          .then(function (isInstall) {

            if (isInstall) {
              //TODO
              // install storage service in remote cluaster

            }

            //$scope.status = 'You said the information was "' + answer + '".';
          }, function () {

          });
      };

       /* $scope.mgrCfgEnv = function(version){

        }*/

    $scope.delCfgVer = function(item,idx,colIdx,e){
      e.stopPropagation();
        var _name = item.cfgMgrId;
        var _ver = item.versionList[idx].imgVersion;
      $mdDialog.show({
        controller: function (item,idx,colIdx,configFileSets,$scope) {
          var param = {
            imageName:item.cfgMgrId,
            imageVersion:[item.versionList[idx].imgVersion],
            files:item.versionList[idx].files
          }
          var _name = item.cfgMgrId;
          $scope.cName=item.cfgMgrId;
          var _ver = item.versionList[idx].imgVersion;
          $scope.ok=function () {
            configurateService.delImgVersionCfg(param).then(function(ret){

                $mdDialog.hide(ret);
            },function(err){
                $mdDialog.cancel(err);
                //alertify.error('delete image ['+_name+']:['+_ver+'] error:'+err.data.data);
            });

          }
          $scope.cancel=function () {
            $mdDialog.cancel();
          }

        },
        templateUrl: 'delConfigVar.html',
        parent: angular.element(document.body),
        targetEvent: e,
        clickOutsideToClose: true,
        fullscreen: true,// Only for -xs, -sm breakpoints.
        locals: {
          item:item,
          idx:idx,
          colIdx:colIdx,
          configFileSets:$scope.configFileSets
        }
      })
      .then(function(ret){
          if(ret.data.ret === true){
              item.versionList.splice(idx,1);
              /*if(item.versionList.length === 0){
               var _idx = $scope.configFileSets.index(item);
               $scope.currentPageCfgSet.splice(_idx,1);
               }*/

              alertify.success("delete image ["+_name+"]:["+_ver+"] config success");
          }else{
              alertify.error('delete image ['+_name+']:['+_ver+'] error:'+ret.data.data);
          }
      },function(err){
          if(err)
              alertify.error('delete image ['+_name+']:['+_ver+'] error:'+err.data.data);
      })




    }

    $scope.delCfgAllVer = function(e,item,idx){
        e.stopPropagation();


      $mdDialog.show({
        controller: function (item,idx,configFileSets,$scope) {
          var param = {
            imageName:item.cfgMgrId,
            imageVersion:[],
            files:[]
          };
          item.versionList.forEach(function(one){
            param.imageVersion.push(one.imgVersion);
            param.files = param.files.concat(one.files);
          });
          $scope.cName=param.imageName;
          $scope.ok=function () {
            configurateService.delImgVersionCfg(param).then(function(ret){

                $mdDialog.hide(ret);
            },function(err){
              //alertify.error('delete image ['+item.cfgMgrId+'] error:'+err.data.data);
                $mdDialog.cancel(err);
            });

          }
          $scope.cancel=function () {
            $mdDialog.cancel();
          }
        },
        templateUrl: 'delConfigGroup.html',
        parent: angular.element(document.body),
        targetEvent: e,
        clickOutsideToClose: true,
        fullscreen: true,// Only for -xs, -sm breakpoints.
        locals: {
          item:item,
          idx:idx,
          configFileSets:$scope.configFileSets
        }
      })
      .then(function(ret){
          if(ret.data.ret === true){
              $scope.configFileSets.splice(idx,1);
              alertify.success("delete image ["+item.cfgMgrId+"] config success");
          }else{
              alertify.error('delete image ['+item.cfgMgrId+'] error:'+ret.data.data);
          }
      },function(err){
        if(err)
            alertify.error('delete image ['+item.cfgMgrId+'] error:'+err.data.data);
      });
    }
});